package com.ruoyi.job.config;


import com.ruoyi.common.redis.service.RedisService;
import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 发手机短信
 */
@Component
public class PhoneUtil {
    @Autowired
    RedisService redisService;

    public  String send(  String telphone) {
        //产生随机验证码
        int code = (int) (Math.random() * 999999) + 1;
        //redis 默认存储10分钟,10分钟后无法登录
        //redisTemplate.opsForValue().set(telphone, code + "", 10, TimeUnit.MINUTES);
        redisService.setCacheObject(telphone,code+"",10L,TimeUnit.MINUTES);
        ZhenziSmsClient client = new ZhenziSmsClient("http://sms_developer.zhenzikj.com", "110**", "自己密码");//https://sms_developer.zhenzikj.com
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("number", telphone);
        params.put("templateId", "7739");
        String[] templateParams = new String[2];
        templateParams[0] = code + "";
        templateParams[1] = "10分钟";
        params.put("templateParams", templateParams);
        String result = null;
        try {
//             发送短信
//            result = client.send(params);
            result="{'code':0,data:'发送成功'}";
            System.out.println("手机短信发送："+result+"验证码是："+code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
